package de.zainodis.commons.communication.local;

import static junit.framework.Assert.assertNotNull;
import de.zainodis.commons.utils.StringUtils;

/**
 * {@link MessagePortSubscriber} can subscribe to {@link MessagePort}s. The
 * receiver method of a message port subscriber must be public, static and it's
 * parameters must match the parameter list defined by the {@link MessagePort}
 * that should be subscribed to.
 * 
 * @author zainodis
 * 
 */
public class MessagePortSubscriber {

   private Class receiverClass;
   /** Receiver methods must be public, static */
   private String receiverMethod;

   public MessagePortSubscriber(Class receiverClass, String receiverMethod) {
	 assertNotNull("Receiver class of a message port subscriber must not be null.", receiverClass);
	 assertNotNull("Receiver method of a message port subscriber must not be null.",
		  receiverMethod);
	 this.receiverClass = receiverClass;
	 this.receiverMethod = receiverMethod;
   }

   public Class getReceiverClass() {
	 return receiverClass;
   }

   public String getReceiverMethod() {
	 return receiverMethod;
   }

   @Override
   public String toString() {
	 return String.format("Receiver Class: %s Receiver Method: %s",
		  receiverClass.getCanonicalName(), receiverMethod);
   }

   @Override
   public boolean equals(Object o) {
	 if (o == null) {
	    return false;
	 }
	 return StringUtils.equals(toString(), o.toString());
   }

   @Override
   public int hashCode() {
	 // Required for comparing messagePortSubscribers in a set
	 return toString().hashCode();
   }
}
